/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.aiwei.tdjk.service.impl;


import com.aiwei.tdjk.entity.User;
import com.aiwei.tdjk.service.PasswordService;
import com.aiwei.tdjk.utils.security.Md5Utils;
import org.springframework.stereotype.Service;

/**
 * @Author yanfeng
 */
@Service
public class PasswordServiceImpl implements PasswordService {

//    @Autowired
//    private CacheManager ehcacheManager;
//
//    private Cache loginRecordCache;

//    @Value(value = "${user.password.maxRetryCount}")
    private int maxRetryCount = 10;

    public void setMaxRetryCount(int maxRetryCount) {
        this.maxRetryCount = maxRetryCount;
    }

//    @PostConstruct
//    public void init() {
//        loginRecordCache = ehcacheManager.getCache("loginRecordCache");
//    }

    @Override
    public boolean matches(User user, String newPassword) {
        return user.getPassword().equals(encryptPassword(user.getUsername(), newPassword, user.getSalt()));
    }

    @Override
    public String encryptPassword(String username, String password, String salt) {
        return Md5Utils.hash(username + password + salt);
    }


    public static void main(String[] args) {
        System.out.println(new PasswordServiceImpl().encryptPassword("monitor", "123456", "iY71e4d123"));
    }
}
